package com.laker.autotest.desktop;

import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import lombok.Data;

@Data
public class ScriptEvent {
    private EventType type;
    private long timestamp;
    private Integer x;
    private Integer y;
    private Integer x2;
    private Integer y2;
    private Integer keyCode;
    private Integer button;
    private String imagePath;
    private String region;
    private KeyEvent keyEvent;
    private Integer wheelRotation;


    public ScriptEvent() {
    }

    public ScriptEvent(EventType type, Long timestamp) {
        this.type = type;
        this.timestamp = timestamp;
    }

    public static ScriptEvent mouseMove(int x, int y) {
        ScriptEvent e = new ScriptEvent(EventType.MOUSE_MOVE, System.currentTimeMillis());
        e.x = x;
        e.y = y;
        return e;
    }

    public static ScriptEvent mouseClick(int x, int y, int button) {
        ScriptEvent e = new ScriptEvent(EventType.MOUSE_CLICK, System.currentTimeMillis());
        e.x = x;
        e.y = y;
        e.button = button;
        return e;
    }

    public static ScriptEvent keyPress(NativeKeyEvent nativeKeyEvent) {
        ScriptEvent e = new ScriptEvent(EventType.KEY_PRESS, System.currentTimeMillis());
        e.setKeyEvent(new KeyEvent(nativeKeyEvent.getID(),
                nativeKeyEvent.getModifiers(),
                nativeKeyEvent.getRawCode(),
                nativeKeyEvent.getKeyCode(),
                nativeKeyEvent.getKeyChar(),
                nativeKeyEvent.getKeyLocation()));
        return e;
    }

    public static ScriptEvent keyRelease(NativeKeyEvent nativeKeyEvent) {
        ScriptEvent e = new ScriptEvent(EventType.KEY_RELEASE, System.currentTimeMillis());
        e.setKeyEvent(new KeyEvent(nativeKeyEvent.getID(),
                nativeKeyEvent.getModifiers(),
                nativeKeyEvent.getRawCode(),
                nativeKeyEvent.getKeyCode(),
                nativeKeyEvent.getKeyChar(),
                nativeKeyEvent.getKeyLocation()));
        return e;
    }

    public static ScriptEvent keyTyped(NativeKeyEvent nativeKeyEvent) {
        ScriptEvent e = new ScriptEvent(EventType.KEY_TYPED, System.currentTimeMillis());
        e.setKeyEvent(new KeyEvent(nativeKeyEvent.getID(),
                nativeKeyEvent.getModifiers(),
                nativeKeyEvent.getRawCode(),
                nativeKeyEvent.getKeyCode(),
                nativeKeyEvent.getKeyChar(),
                nativeKeyEvent.getKeyLocation()));
        return e;
    }

    public static ScriptEvent screenshot(String imagePath, String region) {
        ScriptEvent e = new ScriptEvent(EventType.SCREENSHOT, System.currentTimeMillis());
        e.imagePath = imagePath;
        e.region = region;
        return e;
    }

    public static ScriptEvent scroll(int x, int y, int x2, int y2) {
        ScriptEvent e = new ScriptEvent(EventType.SCROLL, System.currentTimeMillis());
        e.x = x;
        e.y = y;
        e.x2 = x2;
        e.y2 = y2;
        return e;
    }

    public static ScriptEvent mouseWheel(int x, int y, int wheelRotation) {
        ScriptEvent e = new ScriptEvent(EventType.MOUSE_WHEEL, System.currentTimeMillis());
        e.x = x;
        e.y = y;
        e.wheelRotation = wheelRotation;
        return e;
    }

    public static ScriptEvent mousePress(int x, int y, int button) {
        ScriptEvent e = new ScriptEvent(EventType.MOUSE_PRESS, System.currentTimeMillis());
        e.x = x;
        e.y = y;
        e.button = button;
        return e;
    }

    public static ScriptEvent mouseRelease(int x, int y, int button) {
        ScriptEvent e = new ScriptEvent(EventType.MOUSE_RELEASE, System.currentTimeMillis());
        e.x = x;
        e.y = y;
        e.button = button;
        return e;
    }

    public static ScriptEvent mouseDrag(int x, int y, int x1, int y1) {
        ScriptEvent e = new ScriptEvent(EventType.MOUSE_DRAG, System.currentTimeMillis());
        e.x = x;
        e.y = y;
        e.x2 = x1;
        e.y2 = y1;
        return e;
    }

    public void setType(String type) {
        if (type != null) {
            try {
                this.type = EventType.valueOf(type);
            } catch (Exception e) {
                this.type = null;
            }
        } else {
            this.type = null;
        }
    }
}
